#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int n,m;
const int N=1e3+10;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};

int numset[N][N];
int dist[N][N];
typedef struct node
{
    int Weight;
    int x,y;
    bool operator <(const node&other) const 
    {
        return Weight >other.Weight;
    }
}node;

int dijkstra()
{
    memset(dist,0x3f,sizeof dist);
    priority_queue<node> pq;

    for(int i=1;i<=m;i++)
    {
        pq.push({numset[1][i],1,i});
        dist[1][i]=numset[1][i];
    }

    while(pq.size())
    {
        auto random=pq.top();
        int maxval=random.Weight;
        int x=random.x;
        int y=random.y;
        pq.pop();

        if(maxval >dist[x][y]) continue;
        if(x==n) return maxval;

        for(int i=0;i<4;i++)
        {
            int a=x+dx[i];
            int b=y+dy[i];

            if(a>=1&&a<=n&&b<=m&&b>=1)
            {
                int newmax=max(numset[a][b],maxval);
                if(dist[a][b]>newmax)
                {
                    dist[a][b]=newmax;
                    pq.push({newmax,a,b});
                }
            }
        }
    }
    return -1;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>numset[i][j];
        }
    }

    cout<<dijkstra()<<endl;
}